package com.example.mainstream.adapter;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.shopapp.R;
import com.example.shopapp.bean.Order;
import com.example.shopapp.utils.Utils;

/**
 * 文件名：ShopCarAdapter
 * 作  者： admin
 * 描述：TOOD
 */
public class ShopCarAdapter extends BaseQuickAdapter<Order, BaseViewHolder> {
    private Context context;
    private OnShopCarListener listener;
    public ShopCarAdapter(Context context, OnShopCarListener listener) {
        super(R.layout.item_spc_goods);
        this.context = context;
        this.listener = listener;
    }

    @Override
    protected void convert(BaseViewHolder helper, Order item) {
        helper.setText(R.id.spc_tv_shop_name_msg,item.getTitle());
        helper.setText(R.id.spc_tv_zong_price,item.getPrice()+"元");
        Glide.with(context).load(item.getUrl()).into((ImageView) helper.getView(R.id.spc_iv_page));
        final CheckBox cbGoods = helper.getView(R.id.spc_cb_goods);
        cbGoods.setChecked(item.isIsselect());
        cbGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Utils.orderList.get(Utils.getGoodIdByGoods(item)).setIsselect(!item.isIsselect());
                if(listener != null){
                    listener.onPayClick();
                }
            }
        });
        helper.setText(R.id.spc_et_comm_count,item.getCount()+"");
        helper.getView(R.id.spc_btn_comm_count_jia).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int count = Utils.orderList.get(Utils.getGoodIdByGoods(item)).getCount() + 1;
                Utils.orderList.get(Utils.getGoodIdByGoods(item)).setCount(count);
                if(!item.isIsselect()){
                    Toast.makeText(context,"请选选择商品",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(listener != null){
                    listener.onPayClick();
                }
            }
        });
        helper.getView(R.id.spc_btn_comm_count_jian).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Utils.orderList.get(Utils.getGoodIdByGoods(item)).getCount()  > 1){
                    int count = Utils.orderList.get(Utils.getGoodIdByGoods(item)).getCount() - 1;
                    Utils.orderList.get(Utils.getGoodIdByGoods(item)).setCount(count);
                }else {
                    Utils.orderList.get(Utils.getGoodIdByGoods(item)).setCount(1);
                }
                if(!item.isIsselect()){
                    Toast.makeText(context,"请选选择商品",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(listener != null){
                    listener.onPayClick();
                }
            }
        });
        helper.getView(R.id.rl).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (listener!=null){
                    listener.onLongClick(item);
                }
                return false;
            }
        });
    }
}
